import { request } from "../../request/index.js"
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMenuList: [],
    rightMenuList: [],
    currentIndex: 0,
    top: 0
  },

  Cates: [],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const Cates = wx.getStorageSync("cates");

    if (!Cates) {
      this.getCates()
    } else {
      if (Date.now() - Cates.time > 1000 * 10) {
        this.getCates()
      } else {
        this.Cates = Cates.data
        let leftMenuList = this.Cates.map(v => v.cat_name)

        let rightMenuList = this.Cates[0].children;

        this.setData({
          leftMenuList, rightMenuList
        })
      }
    }




  },
  //获取分类数据
  async getCates() {
    // request({ url: "/categories" })
    //   .then(result => {
    //     this.Cates = result.data.message

    //     wx.setStorageSync("cates", { time: Date.now(), data: this.Cates });


    //     let leftMenuList = this.Cates.map(v => v.cat_name)

    //     let rightMenuList = this.Cates[0].children;

    //     this.setData({
    //       leftMenuList, rightMenuList
    //     })
    //   })

    const result = await request({ url: "/categories" })

   // this.Cates = result.data.message
    this.Cates = result

    wx.setStorageSync("cates", { time: Date.now(), data: this.Cates });


    let leftMenuList = this.Cates.map(v => v.cat_name)

    let rightMenuList = this.Cates[0].children;

    this.setData({
      leftMenuList, rightMenuList
    })


  },
  //左侧菜单的点击事件
  hanldeItemTap(e) {
    console.log(e);

    const { index } = e.currentTarget.dataset

    let rightMenuList = this.Cates[index].children;

    this.setData({
      currentIndex: index,
      rightMenuList,
      top: 0
    })



  }

})